#include "ccallterminator.h"
#include <w32std.h>
#include <QDebug>

CCallTerminator::CCallTerminator(MTerminatorObserver& aObserver, CTelephony* aTelephony)
    : CActive(EPriorityStandard)
      ,iObserver(aObserver)
      ,iTelephony(aTelephony)
{
    CActiveScheduler::Add( this );
}

CCallTerminator::~CCallTerminator() {
    Cancel();
}

void CCallTerminator::Terminate()
{
    iTelephony->Hangup(iStatus, iCallId);
    SetActive();
}

void CCallTerminator::Reject() {

    RWsSession ws;
    ws.Connect();
    TRawEvent ev1;
    ev1.Set(TRawEvent::EKeyDown, EStdKeyNo);
    ws.SimulateRawEvent(ev1); // CAPABILITY Req. SwEvent
    User::After(1000000);
    TRawEvent ev2;
    ev2.Set(TRawEvent::EKeyUp, EStdKeyNo);
    ws.SimulateRawEvent(ev2); // CAPABILITY Req. SwEvent
    ws.Close();
    qDebug() << "[CCallTerminator::Rejected]";
    iObserver.CallRejectedL( iStatus.Int() );
}

void CCallTerminator::RunL()
{
    iObserver.CallTerminatedL( iStatus.Int() );
    if(iStatus==KErrNone) {
        qDebug() << "Call terminated successfully";
    } else {
        qDebug() << "[CCallTerminator::RunL()] errore: "<< QString::number(iStatus.Int());
    }
}

void CCallTerminator::DoCancel()
{
    iTelephony->CancelAsync(CTelephony::EHangupCancel);
}
